import client from "../db/redis";

export const redisSet = (key: string, value: any, timeout: number = 60 * 60) => {
    if (typeof (value) == 'object') {
        value = JSON.stringify(value)
    }
    client.set(key, value);
    client.expire(key, timeout)
}

export const redisGet = (key: string) => {
    return new Promise(async (resolve, reject) => {
        try {
            const value = await client.get(key);
            if (!value) {
                resolve(null);
                return;
            }
            try {
                resolve(JSON.parse(value))
            } catch (err) {
                resolve(value)
            }
        } catch (error) {
            reject(error);
        }
    })
}